//
//  BM_WeigthVC.m
//  BaoYouYou
//
//  Created by MZY on 16/12/4.
//  Copyright © 2016年 caobo56. All rights reserved.
//

#import "BM_WeigthVC.h"
#import <CoreText/CoreText.h>
#import "BM_WeightResultVC.h"
#import "HTTPRequestManager.h"
#import "BM_Single.h"

@interface BM_WeigthVC ()<UIAlertViewDelegate>{
}

@property (nonatomic, strong) NSMutableArray *textFieldArray;
@property (nonatomic, strong) NSDictionary *dic;

@end

@implementation BM_WeigthVC

- (UILabel *)labelFrame:(CGRect)frame fontsize:(CGFloat)fontsize lightText:(NSString *)text{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    label.backgroundColor = [UIColor clearColor];
    label.font = [UIFont systemFontOfSize:fontsize];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:text];
    [attributedString addAttribute:(NSString *)kCTKernAttributeName value:@1 range:NSMakeRange(0,text.length)];
    label.attributedText = attributedString;
    return label;
}

-(UITextField *)createTextField{
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, rateWidth(180) , 30)];
    textField.backgroundColor = UIColorFromRGB(0Xf1f2f3);
    textField.layer.cornerRadius = 3;
    textField.layer.masksToBounds = YES;
    textField.layer.borderWidth = 1;
    textField.layer.borderColor = UIColorFromRGB(0Xe37c7e).CGColor;
    textField.keyboardType = UIKeyboardTypeDecimalPad;
    textField.textColor = UIColorFromRGB(0X1a1a1a);
    textField.textAlignment = NSTextAlignmentLeft;
    return textField;
}

- (UIImage *)getImageWithColor:(UIColor *)color{
    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContext(rect.size);
    CGFloat scale = [[UIScreen mainScreen] scale];
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, scale);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context,[color CGColor]);
    CGContextFillRect(context,rect);
    CGContextSaveGState(context);
    CGContextRestoreGState(context);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


-(void)createView{
    int left = 30;
    int top = 100;
    if (INCH_4) {
        left = 20;
    }

    UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WidthOfScreen, 0)];
    backView.top = 64+10;
    backView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:backView];

    NSArray *array = @[@"头围(HC)",@"腹围(AC)",@"股骨长(FL)",@"双顶径(BPD)"];
    for (int i = 0; i< 4; i++) {
        UILabel *hc_label = [self labelFrame:CGRectMake(rateWidth(left), top, WidthOfScreen, 15) fontsize:15 lightText:array[i]];
        hc_label.textColor = UIColorFromRGB(0X1a1a1a);
        [hc_label sizeToFit];
        [self.view addSubview:hc_label];

        UITextField *textF = [self createTextField];
        textF.centerY = hc_label.centerY;
        textF.left = rateWidth(150);
        [self.view addSubview:textF];
        [_textFieldArray addObject:textF];
        UILabel *cm_label = [self labelFrame:CGRectMake(rateWidth(left), 100, 50, 15) fontsize:15 lightText:@"CM"];
        cm_label.textColor = UIColorFromRGB(0X1a1a1a);
        [cm_label sizeToFit];
        cm_label.left = textF.right+rateWidth(10);
        cm_label.centerY = hc_label.centerY;
        [self.view addSubview:cm_label];
        top = hc_label.bottom+rateWidth(30);
    }
    UILabel *lmp_label = [self labelFrame:CGRectMake(rateWidth(left), top, WidthOfScreen, 15) fontsize:15 lightText:@"孕周(LMP)"];
    lmp_label.textColor = UIColorFromRGB(0X1a1a1a);
    [lmp_label sizeToFit];
    [self.view addSubview:lmp_label];

    UITextField *textF = [self createTextField];
    textF.centerY = lmp_label.centerY;
    textF.left = rateWidth(150);
    textF.width = rateWidth(70);
    [self.view addSubview:textF];
    [_textFieldArray addObject:textF];

    UILabel *w_label = [self labelFrame:CGRectMake(rateWidth(left), 100, rateWidth(40), 15) fontsize:15 lightText:@"W"];
    w_label.textColor = UIColorFromRGB(0X1a1a1a);
    w_label.left = textF.right;
    w_label.centerY = lmp_label.centerY;
    w_label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:w_label];

    UITextField *textF2 = [self createTextField];
    textF2.centerY = lmp_label.centerY;
    textF2.left = w_label.right;
    textF2.width = rateWidth(70);
    [self.view addSubview:textF2];
    [_textFieldArray addObject:textF2];

    UILabel *d_label = [self labelFrame:CGRectMake(rateWidth(left), 100, rateWidth(40), 15) fontsize:15 lightText:@"D"];
    d_label.textColor = UIColorFromRGB(0X1a1a1a);
    d_label.left = textF2.right+rateWidth(10);
    d_label.centerY = lmp_label.centerY;
    [self.view addSubview:d_label];

    UIButton *sureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    sureBtn.frame = CGRectMake(0, 0, 180, 44);
    sureBtn.centerX = WidthOfScreen/2;
    sureBtn.top = d_label.bottom+rateWidth(50);
    [sureBtn setTitle:@"体重预测" forState:UIControlStateNormal];
    [sureBtn setBackgroundImage:[self getImageWithColor:UIColorFromRGB(0xE77A7D)] forState:UIControlStateNormal];
    sureBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:sureBtn];
    [sureBtn addTarget:self action:@selector(httpRequest) forControlEvents:UIControlEventTouchUpInside];

    if (INCH_4) {
        backView.height = 333;
        sureBtn.bottom = backView.bottom- 20;
    }else if (INCH_47){
        backView.height = 395;
        sureBtn.bottom = backView.bottom- 50;
    }else{
        backView.height = 421;
        sureBtn.bottom = backView.bottom- 50;
    }

}

-(void)pushResult{
    BM_WeightResultVC *resultVC = [[BM_WeightResultVC alloc]init];
    resultVC.resultInfo = _dic;
    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleDone target:nil action:nil];
    [self.navigationController pushViewController:resultVC animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"胎儿体重预测";
    self.view.backgroundColor = UIColorFromRGB(0Xf1f2f3);
    _textFieldArray = [[NSMutableArray alloc]initWithCapacity:7];
    [self createView];
}

-(void)saveText{
    BM_Single *single = [BM_Single sharedManager];
    for (int i = 0; i<6; i++) {
        UITextField *textF1 = _textFieldArray[i];
        if (textF1.text.length) {
            switch (i) {
                case 0:
                    single.hc = textF1.text;
                    break;
                case 1:
                    single.ac = textF1.text;
                    break;
                case 2:
                    single.fl = textF1.text;
                    break;
                case 3:
                    single.bpd = textF1.text;
                    break;
                case 4:
                    single.w = textF1.text;
                    break;
                case 5:
                    single.d = textF1.text;
                    break;
                default:
                    break;
            }
        }
    }
}

-(void)reSetText{
    BM_Single *single = [BM_Single sharedManager];
    if (single.hc.length) {
        UITextField *textF1 = _textFieldArray[0];
        textF1.text = single.hc;
    }
    if (single.ac.length) {
        UITextField *textF1 = _textFieldArray[1];
        textF1.text = single.ac;
    }
    if (single.fl.length) {
        UITextField *textF1 = _textFieldArray[2];
        textF1.text = single.fl;
    }
    if (single.bpd.length) {
        UITextField *textF1 = _textFieldArray[3];
        textF1.text = single.bpd;
    }
    if (single.w.length) {
        UITextField *textF1 = _textFieldArray[4];
        textF1.text = single.w;
    }
    if (single.d.length) {
        UITextField *textF1 = _textFieldArray[5];
        textF1.text = single.d;
    }
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self reSetText];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self saveText];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)httpRequest{
    NSString *isEmpty = @"";
    [self.view endEditing:YES];
    NSArray *array = @[@"请输入头围",@"请输入腹围",@"请输入股骨长",@"请输入双顶径",@"请输入孕周",@"请输入孕周"];
    for (int i = 0; i<6; i++) {
        UITextField *texF = _textFieldArray[i];
        if (!texF.text.length) {
            isEmpty = array[i];
            break;
        }
    }
    if (isEmpty.length) {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示信息" message:isEmpty delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
    }else{
        [self saveText];
        NSString *hc = [(UITextField *)_textFieldArray[0] text];
        NSString *ac = [(UITextField *)_textFieldArray[1] text];
        NSString *fl = [(UITextField *)_textFieldArray[2] text];
        NSString *bpd = [(UITextField *)_textFieldArray[3] text];
        NSString *lmp_w = [(UITextField *)_textFieldArray[4] text];
        NSString *lmp_d = [(UITextField *)_textFieldArray[5] text];

        NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
        NSString * userid = @"0";
        if ([user valueForKey:@"userid"]) {
            userid = [NSString stringWithFormat:@"%@",[user valueForKey:@"userid"]];
        }

        NSString *url = [NSString stringWithFormat:@"http://183.174.228.55:8089/youbaobao/app/weightProb?userId=%@&HC=%@&AC=%@&FL=%@&BPD=%@&LMP_W=%@&LMP_D=%@",userid,hc,ac,fl,bpd,lmp_w,lmp_d];
        WeakSelf
        [self.view showLoadingWithMessage:nil];
        [[HTTPRequestManager sharedManager] addGETRequestParameters:url parametes:nil success:^(id successobject) {
            [weakSelf.view hideLodingView];
            NSLog([successobject description], nil);
            _dic = successobject;
            [self pushResult];
        } fail:^(id failobject) {
            [weakSelf.view hideLodingView];
            [weakSelf.view toastMessage:@"网络请求错误，请稍后重试"];
        }];
    }

}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{

}

@end
